package com.treasure.mall.biz.vo.module;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.treasure.mall.biz.bo.module.HomeConfig;
import com.treasure.mall.biz.bo.module.HomeIconConfig;
import com.treasure.mall.biz.bo.module.HomeTodayNewConfig;
import com.treasure.mall.zbq.common.ienum.banner.ModuleType;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.vo.module
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/8/10 10:26 上午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * 
 */
@Data
public class ModuleVO {

    private Long id;
    /**
     * 定时时间
     */
    private LocalDateTime timingTime;
    /**
     * 定时更新内容
     */
    private String timingContent;
    /**
     * 模块类型
     */
    private ModuleType moduleType = ModuleType.MODULE_HOME;
//    /**
//     * 模块名称
//     */
//    private String moduleName;

    /**
     * 模块属性配置
     */
    @JsonIgnore
    private String properties;


    private Object adConfig;

    @JsonIgnore
    public HomeConfig getHomeConfig() {
        if (StringUtils.isNotBlank(properties)) {
            if (moduleType.equals(ModuleType.MODULE_HOME)) {
                return JSONObject.parseObject(properties, HomeConfig.class);
            }
        }
        return new HomeConfig();
    }

    @JsonIgnore
    public List<HomeIconConfig> getHomeIconConfig() {
        List<HomeIconConfig> configs = new ArrayList<>();
        if (StringUtils.isNotBlank(properties)) {
            if (moduleType.equals(ModuleType.MODULE_ICON)) {
                configs = JSONObject.parseArray(properties, HomeIconConfig.class);
            }
        } else {
            configs.add(new HomeIconConfig("", "", ""));
            configs.add(new HomeIconConfig("", "", ""));
        }
        return configs;
    }


    @JsonIgnore
    public List<HomeTodayNewConfig> getTodayNewConfig() {
        List<HomeTodayNewConfig> configs = new ArrayList<>();
        if (StringUtils.isNotBlank(properties)) {
            if (moduleType.equals(ModuleType.MODULE_TODAY_NEW)) {
                configs = JSONObject.parseArray(properties, HomeTodayNewConfig.class);
            }
        } else {
            configs.add(new HomeTodayNewConfig("", "", ""));
            configs.add(new HomeTodayNewConfig("", "", ""));
            configs.add(new HomeTodayNewConfig("", "", ""));
            configs.add(new HomeTodayNewConfig("", "", ""));
        }
        return configs;
    }


    /**
     * 获取专区信息
     *
     * @return
     */
    public Object getAdConfig() {
        if (StringUtils.isNotBlank(properties)) {
            if (moduleType.equals(ModuleType.MODULE_HOME)) {
                return JSONObject.parseObject(properties, HomeConfig.class);
            }
            if (moduleType.equals(ModuleType.MODULE_ICON)) {
                return JSONObject.parseArray(properties, HomeIconConfig.class);
            }
            if (moduleType.equals(ModuleType.MODULE_TODAY_NEW)) {
                return JSONObject.parseArray(properties, HomeTodayNewConfig.class);
            }
        }
        return null;
    }

}
